/**
 * This file is a part of qloudgen-sec. 
 * You can redistribute qloudgen-sec and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-sec is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.sec.cas.support.user.userdetail.web;
 
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

import org.springframework.web.servlet.ModelAndView;  
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;  

import org.qloudgen.sec.cas.support.user.services.UserDetail;
import org.qloudgen.sec.cas.support.user.services.UserDetailManager;

import java.util.List;  
import java.util.Map;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ManageUserDetailMultiActionController extends MultiActionController{  
	private static final Logger logger = LoggerFactory.getLogger(ManageUserDetailMultiActionController.class);
  
    /** View name for User View. */
    private static final String VIEW_NAME_MANAGE = "manageUserDetailView";

    private UserDetailManager userDetailManager;  
    
    public ManageUserDetailMultiActionController(final UserDetailManager userDetailManager){
    	  super();
    	  this.userDetailManager = userDetailManager;
    }
    
    public UserDetailManager getUserDetailManager() {  
        return userDetailManager;  
    }  
  
    public void setUserDetailManager(UserDetailManager userDetailManager) {  
        this.userDetailManager = userDetailManager;  
    }  
  

    public ModelAndView manageUserDetail(final HttpServletRequest request,
            final HttpServletResponse response) throws Exception {  
        // TODO Auto-generated method stub  
        
        final Map<String, Object> model = new HashMap<String, Object>();
        
        final List<UserDetail> list = this.userDetailManager.getAllUserDetail();
        
        logger.debug("**********************************");
        logger.debug("userDetail count:"+list.size());
        logger.debug("**********************************");
        
        model.put("userDetail", list);
        model.put("pageTitle", VIEW_NAME_MANAGE);
        
        return new ModelAndView(VIEW_NAME_MANAGE, model);  
    }     
    
    
    public ModelAndView deleteUserDetail(final HttpServletRequest request, 
            final HttpServletResponse response) throws Exception {
        logger.debug("start deleteUserDetail");
        final String id = request.getParameter("id");
        
        final ModelAndView modelAndView = new ModelAndView(new RedirectView(
                "manageUserDetail.html", true), "status", "deleted");
       
        final UserDetail r = this.userDetailManager.delete(id);
        
         modelAndView.addObject("colName", r != null
                ? r.getColName() : "");
        logger.debug("end deleteUserDetail");
        return modelAndView;
    }
} 
